package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Switch;

public class EditActivity extends AppCompatActivity {
    private EditText nameEdit;
    private EditText ageEdit;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private RadioButton radioButton4;
    private RadioButton radioButton5;
    private Button btnSaveStu;
    private String name;
    private int age;
    private int headId;
    private int no;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        setTitle("编辑学生数据");

        findViews();

        initData();

        setSaveClickListener();
    }


    /**
     * 点击保存事件
     */
    private void setSaveClickListener() {
        btnSaveStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameEdit.getText().toString();
                int age = Integer.parseInt(ageEdit.getText().toString());

                int imgId = R.drawable.dog1;
                if(radioButton1.isChecked()){
                    imgId = R.drawable.dog1;
                }else if(radioButton2.isChecked()){
                    imgId = R.drawable.dog2;
                }else if(radioButton3.isChecked()){
                    imgId = R.drawable.dog3;
                }else if(radioButton4.isChecked()){
                    imgId = R.drawable.dog4;
                }else if(radioButton5.isChecked()){
                    imgId = R.drawable.dog5;
                }


                Intent intent = new Intent();
                intent.putExtra("NAME",name);
                intent.putExtra("AGE",age);
                intent.putExtra("IMG",imgId);
                intent.putExtra("NO",no);

                setResult(20010,intent);


                finish();
            }
        });
    }

    /**
     * 初始化页面内容
     */
    private void initData() {
        Intent intent = getIntent();
        name = intent.getStringExtra("NAME");
        age = intent.getIntExtra("AGE",0);
        headId = intent.getIntExtra("IMG",0);
        no = intent.getIntExtra("NO",0);
        nameEdit.setText(name);
        ageEdit.setText(String.valueOf(age));

        switch (headId){
            case R.drawable.dog1:
                radioButton1.setChecked(true);
                break;
            case R.drawable.dog2:
                radioButton2.setChecked(true);
                break;
            case R.drawable.dog3:
                radioButton3.setChecked(true);
                break;
            case R.drawable.dog4:
                radioButton4.setChecked(true);
                break;
            case R.drawable.dog5:
                radioButton5.setChecked(true);
                break;
                    }
    }


    /**
     * 初始化所有视图
     */
    private void findViews() {
        nameEdit = findViewById(R.id.name_edit);
        ageEdit = findViewById(R.id.age_edit);

        radioButton1 = findViewById(R.id.radio_btn1);
        radioButton2 = findViewById(R.id.radio_btn2);
        radioButton3 = findViewById(R.id.radio_btn3);
        radioButton4 = findViewById(R.id.radio_btn4);
        radioButton5 = findViewById(R.id.radio_btn5);
        btnSaveStu = findViewById(R.id.save_btn);
    }
}
